<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <el-row>
        <span>{{ treeData.name }}</span>
      </el-row>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeData.manager }}</el-col>
        <el-col>
          <el-dropdown>
            <span>
              操作
              <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="$emit('openDialog', treeData.id)"
              >
                添加子部门
              </el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                @click.native="$emit('editDialog', treeData.id)"
              >
                编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepart">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentApi } from '@/api/department'
export default {
  name: 'TreeTools',
  props: {
    // treeData是这个组件的数据,就是树组件的data数据
    treeData: {
      type: Object,
      default: () => {} // 默认值是个空对象
    },

    // 是否为一级
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async delDepart() {
      try {
        // 提示弹框
        await this.$confirm('确定要删除这条数据嘛')
        // 调接口，删数据
        await delDepartmentApi(this.treeData.id)
        // 提示删除成功
        this.$message.success('删除成功')
        // 重新渲染
        this.$emit('updateList')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style></style>
